#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

# DESCRIPTION Data integrity after turning off cache device

# Standard beginning for every test - get the main tests directory and
# link the cas_lib file for CAS API, then use "start_test $*" to pass params
# and do other necessary checks and setup
# USE_IN_BVT
# USE_IN_NIGHTLY

TESTS_DIR="$(dirname $0)/../"
. $TESTS_DIR/cas_lib
start_test $*

# This is where the real test starts

# Use CACHE_DEVICE and CORE_DEVICE provided by configuration file and remove partitions from those devices
TARGET_DEVICE_OPTION="$CACHE_DEVICE" remove_partitions
TARGET_DEVICE_OPTION="$CORE_DEVICE" remove_partitions

# Create 3 primary partitions on CACHE_DEVICE, each of 2000M size
TARGET_DEVICE_OPTION="$CACHE_DEVICE" PARTITION_SIZE_OPTION="2000M" PARTITION_IDS_OPTION="1 2 3" make_primary_partitions
# Create 3 primary partitions on CORE_DEVICE, each of 4000M size
TARGET_DEVICE_OPTION="$CORE_DEVICE" PARTITION_SIZE_OPTION="4000M" PARTITION_IDS_OPTION="1 2 3" make_primary_partitions

# Start cache on CACHE_DEVICE1 (/dev/sdd1, for example) with ID=1 and add a core device using CORE_DEVICE1 (/dev/sde1, for example)
CACHE_ID_OPTION="1" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part1" CACHE_MODE_OPTION="wb" start_cache
CACHE_ID_OPTION="1" CORE_DEVICE_OPTION="${CORE_DEVICE}-part1" add_core

# Start cache on CACHE_DEVICE2 (/dev/sdd2, for example) with ID=2 and add a core device using CORE_DEVICE2 (/dev/sde2, for example)
CACHE_ID_OPTION="2" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part2" CACHE_MODE_OPTION="wb" start_cache
CACHE_ID_OPTION="2" CORE_DEVICE_OPTION="${CORE_DEVICE}-part2" add_core

# Start cache on CACHE_DEVICE3 (/dev/sdd3, for example) with ID=3 and add a core device using CORE_DEVICE3 (/dev/sde3, for example)
CACHE_ID_OPTION="3" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part3" CACHE_MODE_OPTION="wb" start_cache
CACHE_ID_OPTION="3" CORE_DEVICE_OPTION="${CORE_DEVICE}-part3" add_core

# Create filesystems on cached devices - we do this using run_cmd because it is not in the API (and probably won't be).
# The test framework will accept invoking the commands directly (e.g. "mkfs.ext3 [...]" without the "run_cmd"), but the
# results have to be checked manually by the test.
TARGET_DEVICE_OPTION="${DEVICE_NAME}1-1" FILESYSTEM_TYPE="ext3" make_filesystem
TARGET_DEVICE_OPTION="${DEVICE_NAME}2-1" FILESYSTEM_TYPE="ext4" make_filesystem
TARGET_DEVICE_OPTION="${DEVICE_NAME}3-1" FILESYSTEM_TYPE="xfs" make_filesystem

# Specify a temporary file used for md5sums - note that it resides in $TMP_DIR, which is a special directory defined in cas_config.
# Everytime we use temporary files, they should be placed in that special directory. Its contents are cleared after every test.
MD5_FILE="$TMP_DIR/cas_md5_sum_"

# Mount the filesystems, then create the example files and save their MD5 sums locally in the temporary place
# Note the usage of ${MOUNTPOINT} - mount_cache always mounts using following formula: ${MOUNTPOINT}-${CACHE_ID_OPTION}-${CORE_ID_OPTION}.
for ID in 1 2 3 ; do
    CACHE_ID_OPTION="$ID" CORE_ID_OPTION="1" mount_cache
    run_cmd "dd if=/dev/urandom of=${MOUNTPOINT}-${ID}-1/test bs=50M count=1"
    run_cmd "md5sum ${MOUNTPOINT}-${ID}-1/test > ${MD5_FILE}_${ID}"
done

# Umount & stop the caches, then mount the core devices
# Note the usage of ${MOUNTPOINT} - mount_cache always mounts using following formula: ${MOUNTPOINT}-${CACHE_ID_OPTION}-${CORE_ID_OPTION}.
for ID in 1 2 3 ; do
    run_cmd "umount ${MOUNTPOINT}-${ID}-1"
done

SHORT_LINK=$(realpath ${CACHE_DEVICE})
CACHE_DEVICE_OPTION="${CACHE_DEVICE}" turn_off_device
for ID in 1 2 3 ; do
    DONT_FAIL_ON_ERROR_OPTION="YES" CACHE_DONT_FLUSH_DATA_OPTION="1" CACHE_ID_OPTION="$ID" stop_cache
done
CACHE_DEVICE_OPTION="${SHORT_LINK}" turn_on_device

for ID in 1 2 3 ; do
    CACHE_ID_OPTION="$ID" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part${ID}" CACHE_LOAD_METADATA_OPTION="y" CACHE_MODE_OPTION="wb" start_cache
    CACHE_ID_OPTION="$ID" stop_cache
    run_cmd "mount ${CORE_DEVICE}-part${ID} ${MOUNTPOINT}-${ID}-1"
done

# Now check for files' presence and umount core devices
# Note the usage of ${MOUNTPOINT} - mount_cache always mounts using following formula: ${MOUNTPOINT}-${CACHE_ID_OPTION}-${CORE_ID_OPTION}.
for ID in 1 2 3 ; do
    run_cmd "test -f ${MOUNTPOINT}-${ID}-1/test"
    run_cmd "md5sum -c ${MD5_FILE}_${ID}"
    run_cmd "umount ${MOUNTPOINT}-${ID}-1"
done

# Always return 0 at the end of the test - if at any point something has failed
# in the API functions, test will end and return a proper result.
# If you need to check other things during the test and end the test earlier, you
# should end the test using "end_test $retval" function
end_test 0
CACHE_ID_OPTION="1" CACHE_DEVICE_OPTION="${CACHE_DEVICE:4}" dirty_stop
